package service

import (
	"admin100/app/dao"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/utils"

	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

var TenMinuteStatistics = tenMinuteStatisticsService{}

type tenMinuteStatisticsService struct {
}

func (s *tenMinuteStatisticsService) GetThatDayMaxOnlineCount(platformId, serverId string, channelList []string, startTime, endTime int) int {
	r, err := dao.TenMinuteStatistics.
		Fields("max(" + dao.TenMinuteStatistics.Columns.OnlineCount + ")").Where(gdb.Map{
		dao.TenMinuteStatistics.Columns.PlatformId:                platformId,
		dao.TenMinuteStatistics.Columns.ServerId:                  serverId,
		dao.TenMinuteStatistics.Columns.Channel:                   channelList,
		dao.TenMinuteStatistics.Columns.Time + " between ? and ?": g.Slice{startTime, endTime},
	}).FindOne()
	defErr.IfPanic(err, define.DataOptFail)
	return r.OnlineCount
}

func (s *tenMinuteStatisticsService) Get24hoursOnlineCount(platformId string, serverId string, channelList []string, zeroTimestamp int) ([]string, int) {
	var (
		onlineCountList = make([]string, 144)
		now             = gtime.Now().Unix()
		nowOnline       = 0
	)

	res, err := dao.TenMinuteStatistics.Fields("sum("+dao.TenMinuteStatistics.Columns.OnlineCount+")").WhereValNotEmpty(g.Map{
		dao.TenMinuteStatistics.Columns.PlatformId:                platformId,
		dao.TenMinuteStatistics.Columns.ServerId:                  serverId,
		dao.TenMinuteStatistics.Columns.Channel:                   channelList,
		dao.TenMinuteStatistics.Columns.Time + " between ? and ?": g.Slice{zeroTimestamp, zeroTimestamp + 86400},
		dao.TenMinuteStatistics.Columns.Time + " < ?":             now,
	}).Where(dao.TenMinuteStatistics.Columns.OnlineCount+" > ?", 0).Group(dao.TenMinuteStatistics.Columns.Time).FindArray()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	for k, v := range res {
		num := gconv.Int(v.Val())
		nowOnline += num
		onlineCountList[k] = gconv.String(num)
	}

	return onlineCountList, nowOnline

}

func (s *tenMinuteStatisticsService) Get24hoursRegisterCount(platformId string, serverId string, channelList []string, zeroTimestamp int) ([]string, int) {
	var (
		onlineCountList = make([]string, 144)
		now             = gtime.Now().Unix()
		nowOnline       = 0
	)

	res, err := dao.TenMinuteStatistics.Fields("sum("+dao.TenMinuteStatistics.Columns.RegisterCount+")").WhereValNotEmpty(g.Map{
		dao.TenMinuteStatistics.Columns.PlatformId:                platformId,
		dao.TenMinuteStatistics.Columns.ServerId:                  serverId,
		dao.TenMinuteStatistics.Columns.Channel:                   channelList,
		dao.TenMinuteStatistics.Columns.Time + " between ? and ?": g.Slice{zeroTimestamp, zeroTimestamp + 86400},
		dao.TenMinuteStatistics.Columns.Time + " < ?":             now,
	}).Where(dao.TenMinuteStatistics.Columns.OnlineCount+" > ?", 0).Group(dao.TenMinuteStatistics.Columns.Time).FindArray()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	for k, v := range res {
		num := gconv.Int(v.Val())
		nowOnline += num
		onlineCountList[k] = gconv.String(num)
	}

	return onlineCountList, nowOnline

}

func (s *tenMinuteStatisticsService) Get24hoursChargeCount(platformId string, serverId string, channelList []string, zeroTimestamp int) ([]string, int) {
	var (
		chargeCountList = make([]string, 144)
		now             = gtime.Now().Unix()
		totalCount      = 0
	)

	res, err := dao.TenMinuteStatistics.Fields("sum(" + dao.TenMinuteStatistics.Columns.ChargeCount + ")").WhereValNotEmpty(g.Map{
		dao.TenMinuteStatistics.Columns.PlatformId:                platformId,
		dao.TenMinuteStatistics.Columns.ServerId:                  serverId,
		dao.TenMinuteStatistics.Columns.Channel:                   channelList,
		dao.TenMinuteStatistics.Columns.Time + " between ? and ?": g.Slice{zeroTimestamp, zeroTimestamp + 86400},
		dao.TenMinuteStatistics.Columns.Time + " < ?":             now,
	}).Group(dao.TenMinuteStatistics.Columns.Time).FindArray()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	for k, _ := range chargeCountList {
		sum := 0
		if len(res) > k {
			sum = gconv.Int(res[k].Val())
		}
		totalCount += sum
		totalCountStr := gconv.String(totalCount)
		chargeCountList[k] = utils.IfTrueStr(totalCountStr == "", "0", totalCountStr)
	}

	return chargeCountList, totalCount
}

func (s *tenMinuteStatisticsService) Get24hoursChargePlayerCount(platformId string, serverId string, channelList []string, zeroTimestamp int) ([]string, int) {
	var (
		chargePlayerCountList = make([]string, 144)
		now                   = gtime.Now().Unix()
		totalCount            = 0
	)

	res, err := dao.TenMinuteStatistics.Fields("sum(" + dao.TenMinuteStatistics.Columns.ChargePlayerCount + ")").WhereValNotEmpty(g.Map{
		dao.TenMinuteStatistics.Columns.PlatformId:                platformId,
		dao.TenMinuteStatistics.Columns.ServerId:                  serverId,
		dao.TenMinuteStatistics.Columns.Channel:                   channelList,
		dao.TenMinuteStatistics.Columns.Time + " between ? and ?": g.Slice{zeroTimestamp, zeroTimestamp + 86400},
		dao.TenMinuteStatistics.Columns.Time + " < ?":             now,
	}).Group(dao.TenMinuteStatistics.Columns.Time).FindArray()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	for k, _ := range chargePlayerCountList {
		sum := 0
		if len(res) > k {
			sum = gconv.Int(res[k].Val())
		}
		totalCount = utils.IfTrueInt(sum > totalCount, sum, totalCount)
		totalCountStr := gconv.String(totalCount)
		chargePlayerCountList[k] = utils.IfTrueStr(totalCountStr == "", "0", totalCountStr)
	}

	return chargePlayerCountList, totalCount
}
